﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Logistics_Service.DTO;
using System.Data;

namespace Logistics_Service.DAO
{
    public class GoiHangDAO
    {
        public string ThemGoiHang(float trongluong, float kichthuoc,string diemdau, string vitriht, string diemden)
        {
            string magh=null;
            string sql = string.Format("insert into GoiHang values('{0}',{1},{2},'{3}','{4}','{5}',{6};select @@identity",1,trongluong,kichthuoc,diemdau,vitriht,diemden,1);
            DataProvider conn = new DataProvider();
            DataTable tb = conn.ExecuteQuery(sql);
            magh = tb.Rows[0][0].ToString();
            return magh;
        }
        public bool CapNhatGoiHang(string magoihang, string vitrikho)
        {
            bool kq;
            string query = string.Format("update goihang set vitrihientai =N'"+vitrikho+"' where ma=N'" + magoihang +"'");
            DataProvider dp = new DataProvider();
            kq = dp.ExecuteNonQuery(query);
            return kq;
        }
        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            GoiHangDTO gh = new GoiHangDTO();
            gh.Ma = dt.Rows[i]["Ma"].ToString();
            gh.KichThuoc = float.Parse(dt.Rows[i]["KichThuoc"].ToString());
            gh.TrongLuong = float.Parse(dt.Rows[i]["TrongLuong"].ToString());
            gh.ViTriHienTai = dt.Rows[i]["ViTriHienTai"].ToString();
            gh.DiemDen = dt.Rows[i]["DiemDen"].ToString();
            gh.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());
            return (object)gh;
        }
        public GoiHangDTO[] DanhSachGoiHang(string makhachhang)
        {
            GoiHangDTO[] arr;
            string query = string.Format("select gh.ma,gh.trongluong, gh.kichthuoc, gh.vitrihientai, gh.diemden, gh.daxoa from goihang gh, donhang dh where gh.ma=dh.magoihang and dh.makhachhang=N'" + makhachhang + "'");
            DataProvider dp = new DataProvider();
            DataTable dt = dp.ExecuteQuery(query);
            int i, n = dt.Rows.Count;
            arr = new GoiHangDTO[n];
            for (i = 0; i < n; i++)
            {
                object gh = GetDataFromDataRow(dt, i);
                arr[i] = (GoiHangDTO)gh;
            }
            return arr;
        }
        public GoiHangDTO GoiHang(string magoihang)
        {
            GoiHangDTO gh;
            string query = string.Format("select * from goihang where ma=N'" + magoihang + "'");
            DataProvider dp = new DataProvider();
            DataTable dt = dp.ExecuteQuery(query);
            object h = GetDataFromDataRow(dt, 0);
            gh = (GoiHangDTO)h;
            return gh;
        }
    }
}